﻿using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {
	public GameObject[] enemyFish;
	public GameObject spawner;
	public GameObject puffer;
	public float timer;
	public  float deathTime;
	public float newtime;
	public static bool startSpawning;
	int i;
	// Use this for initialization
	void Start () {
		timer= 3;
		i =0;
	}
	
	// Update is called once per frame
	void Update () {
	//	DontDestroyOnLoad(transform.gameObject);
		if(startSpawning){
			deathTime += Time.deltaTime * .01f;//.0085f;
					newtime += Time.deltaTime;
			//		Debug.Log(deathTime);
					PufferFish pfRef = puffer.GetComponent<PufferFish>();
					float forceRef = pfRef.Force;
				//	Debug.Log(forceRef);
					//Debug.Log(timer);
					//Debug.Log(Time.time);
				if(timer < newtime){

				i = Random.Range(0,enemyFish.Length) ;
				//also scale and change texture, or just make a prefab for each?

						timer +=Time.deltaTime + .5f - deathTime;
						GameObject enemyFishRef=Instantiate(enemyFish[i], new Vector3(0f,0f, 0f), Quaternion.Euler(new Vector3(90,180,0)))as GameObject;

						if(puffer.rigidbody.velocity.y >3){
					//check for tags
							float rndRange=	Random.Range(spawner.collider.bounds.center.y ,	spawner.collider.bounds.center.y + 30);
							enemyFishRef.transform.position = new Vector3(30 + puffer.transform.position.x,rndRange  ,0);
						}
						if(puffer.rigidbody.velocity.y <-3){
							float rndRange=	Random.Range(spawner.collider.bounds.center.y - 30,	spawner.collider.bounds.center.y );
							enemyFishRef.transform.position = new Vector3(30 + puffer.transform.position.x,rndRange  ,0);

						}
						if(puffer.rigidbody.velocity.y >=-3 && puffer.rigidbody.velocity.y <=3){
							float rndRange=	Random.Range(spawner.collider.bounds.center.y - 20,	spawner.collider.bounds.center.y + 20 );
							enemyFishRef.transform.position = new Vector3(30 + puffer.transform.position.x,rndRange  ,0);
							
						}
				
				}
	}
	}
}
